package com.m3958.firstgwt.server.filter;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Singleton;
import com.m3958.firstgwt.server.types.AppConstants;

@Singleton
public class OldSiteRewriteFilter implements Filter{
	
	private static Map<String, String> urlMap = new HashMap<String, String>();
	
	
	static {
		urlMap.put("/list/wzlist/zwgk", "/section/91925");
		urlMap.put("/list/wzlist/zcfg_flfg", "/section/91977");
		urlMap.put("/list/wzlist/zcfg_gfxwj", "/section/91971");
		urlMap.put("/list/wzlist/zcfg_sfyj", "/section/91973");
		urlMap.put("/list/wzlist/cgdt", "/section/91937");
		urlMap.put("/list/wzlist/cgdt_gzdt", "/section/91975");
		urlMap.put("/list/wzlist/cgdt_dcyj", "/section/91979");
		urlMap.put("/list/wzlist/cgdt_cgfc", "/section/91983");
		urlMap.put("/list/wzlist/dtjs", "/section/91939");
		urlMap.put("/list/wzlist/ryjz", "/section/91947");
		urlMap.put("/list/wzlist/wtcx", "/section/91943");
	}
	
	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String s = req.getParameter(AppConstants.HOST_PARAMETER_NAME);
		if("cg.fh.gov.cn".equals(s) || "cgj.m3958.com".equals(s)){
			String uri = req.getRequestURI();
			if(urlMap.get(uri) != null){
				res.sendRedirect("http://" + s + urlMap.get(uri));
				return;
			}
		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
